package com.qfedu.aunt.commons.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.aunt.commons.exception.BusinessException;
import com.qfedu.aunt.commons.manager.SmsManager;
import com.qfedu.aunt.commons.mapper.AuntSkillMapper;
import com.qfedu.aunt.commons.pojo.AuntSkill;
import com.qfedu.aunt.commons.pojo.InformationEvaluation;
import com.qfedu.aunt.commons.mapper.InformationEvaluationMapper;
import com.qfedu.aunt.commons.result.ResponseCode;
import com.qfedu.aunt.commons.service.InformationEvaluationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.aunt.commons.utils.ExceptionUtils;
import com.qfedu.aunt.commons.utils.StringUtil;
import com.qfedu.aunt.commons.vo.InformationEvaluationVo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 信息评估表 服务实现类
 * </p>
 *
 * @author xiaobobo
 * @since 2022-10-26
 */
@Service
public class InformationEvaluationServiceImpl extends ServiceImpl<InformationEvaluationMapper, InformationEvaluation> implements InformationEvaluationService {

    @Autowired
    private SmsManager smsManager;

    @Autowired
    private InformationEvaluationMapper informationEvaluationMapper;

    @Autowired
    private AuntSkillMapper auntSkillMapper;


    @Override
    public void getCode(String telPhoneNumber) {
        if(StringUtils.isEmpty(telPhoneNumber)){
            throw new BusinessException(ResponseCode.PARAMETER_ERROR.getCode(),
                    ResponseCode.PARAMETER_ERROR.getMessage());
        }
        //程序执行到这里说明电话号码没有问题?(电话号码还要进行验证)
        String regex="1[3,4,5,6,7,8,9][0-9]{9}";
        if(!telPhoneNumber.matches(regex)){
            throw new BusinessException(ResponseCode.TELPNONE_NUM_ERROR.getCode(),
                    ResponseCode.TELPNONE_NUM_ERROR.getMessage());
        }
        //程序执行到这里 说明电话号码是对的
        int code=(int)((Math.random()*9+1)*100000);
        // TODO 这里还需要将验证码存储到Redis中...

        //这个就是验证码
        //调用发短信的程序 进行短信的发送
        smsManager.sendSms(telPhoneNumber,code);
    }

    @Override
    public HashMap<String, List<AuntSkill>> getStaticDictionary() {
        HashMap<String,List<AuntSkill>> maps=new HashMap<>();
        List<Integer> skillTypes=auntSkillMapper.selectAllTypes();
        for (Integer skillType:skillTypes) {
            QueryWrapper<AuntSkill> auntSkillQueryWrapper = new QueryWrapper<>();
            auntSkillQueryWrapper.eq("skill_type",skillType);
            //接下来再查询一次
            List<AuntSkill> auntSkills = auntSkillMapper.selectList(auntSkillQueryWrapper);
            maps.put(String.valueOf(skillType),auntSkills);
        }
        return maps;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(InformationEvaluationVo informationEvaluationVo) throws InvocationTargetException, IllegalAccessException {
        if(null==informationEvaluationVo|| StringUtils.isEmpty(informationEvaluationVo.getArea())||
            StringUtils.isEmpty(informationEvaluationVo.getIeAddress())||
            informationEvaluationVo.getAuntWorkAge()==null||informationEvaluationVo.getIeType()==null
            ){
            throw new BusinessException(ResponseCode.PARAMETER_ERROR.getCode(),
                    ResponseCode.PARAMETER_ERROR.getMessage());
        }
        //程序执行到这里 说明没有问题
        //将vo转换成po
        InformationEvaluation informationEvaluation = new InformationEvaluation();
        BeanUtils.copyProperties(informationEvaluation,informationEvaluationVo);
        //接下来在这里将数据放到数据库
        informationEvaluationMapper.insert(informationEvaluation);
    }
}
